package org.lab.tracecash.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.eclipse.persistence.annotations.Index;
import org.lab.tracecash.common.serialization.NotSerializable;

@Entity
@Table(name = "tc_user")
@SuppressWarnings("serial")
public class User implements Serializable {

	@Id
	@GeneratedValue
	@Column(name = "id")
	private Long id;

	@Column(name = "name", unique = true, length = 32, nullable = false)
	@Index(name = "ix_tc_user_name")
	private String name;

	@Column(name = "email", unique = true, length = 128, nullable = false)
	@Index(name = "ix_tc_user_email")
	private String email;

	@Column(name = "pwd_digest", length = 256, nullable = false)
	@NotSerializable
	private String passwordDigest;

	@Column(name = "created", nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date created;

	@OneToMany(cascade = CascadeType.PERSIST)
	@JoinTable(name = "tc_user_rules")
	@NotSerializable
	private List<TransactionResolverRule> rules;

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	private UserPreferences preferences;

	public User() {
	}

	public User(String name, String email, String passwordDigest, Date created) {
		this.name = name;
		this.email = email;
		this.passwordDigest = passwordDigest;
		this.created = created;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPasswordDigest() {
		return passwordDigest;
	}

	public void setPasswordDigest(String passwordDigest) {
		this.passwordDigest = passwordDigest;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public List<TransactionResolverRule> getRules() {
		return rules;
	}

	public void setRules(List<TransactionResolverRule> rules) {
		this.rules = rules;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public UserPreferences getPreferences() {
		return preferences;
	}

	public void setPreferences(UserPreferences preferences) {
		this.preferences = preferences;
	}
}
